package com.wxy.ideaplugin.utils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class AiResourceUtils {

    public static Map<String, String> parseAiCodeResourceFile(String projectPath) {
        //String file = projectPath+ File.separator+"src"+File.separator+"main"  +File.separator+"resources"+File.separator+"ai_code.properties";
        String file = projectPath + File.separator+ "aicode" + File.separator+"aicode.properties";
        Map<String, String> resourceMap = new HashMap<>();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        if (inputStream != null) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
                String line;
                StringBuilder valSb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (line.startsWith("#"))continue;
                    if (!line.contains("="))continue;
                    String[] lineArr = line.split("=");
                    String key = lineArr[0].trim();
                    valSb.setLength(0);
                    for (int i=1; i<lineArr.length; i++) {
                        valSb.append(lineArr[i].trim());
                    }
                    String val = valSb.toString().trim();
                    if (val.contains("#")) {
                        val = val.substring(0, val.indexOf("#")).trim();
                    }
                    resourceMap.put(key, val);
                }
            } catch (IOException exx) {
                exx.printStackTrace();
            }
        }
        return resourceMap;
    }

}
